/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.discount.web.remote;


import com.medusa.gruul.common.core.annotation.EscapeLogin;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.discount.api.model.vo.api.ApiCouponVo;
import com.medusa.gruul.discount.api.model.vo.manager.FullScaleVo;
import com.medusa.gruul.discount.api.model.vo.manager.MemberCouponVo;
import com.medusa.gruul.discount.service.api.IApiCouponService;
import com.medusa.gruul.discount.service.manager.IFullScaleService;
import com.medusa.gruul.discount.service.manager.IMemberCouponService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 优惠劵、满减 前端控制器
 * </p>
 *
 * @author lcysike
 * @since 2019-12-24
 */
@RestController(value = "remoteDiscountController")
@RequestMapping("/")
public class DiscountController {

    @Autowired
    private IApiCouponService apiCouponService;

    @Autowired
    private IMemberCouponService memberCouponService;

    @Autowired
    private IFullScaleService fullScaleService;


    //=============================================订单部分===================================================

    /**
     * 查询单个客户的可使用优惠劵
     *
     * @param userId
     * @return com.medusa.gruul.common.core.util.Result<java.util.List < com.medusa.gruul.discount.api.model.vo.manager.MemberCouponVo>>
     */
    @GetMapping("get/member/coupon/single")
    @EscapeLogin
    public List<MemberCouponVo> findMemberCouponListForOrder(@RequestParam(value = "userId", required = true) String userId, @RequestParam(value = "productIds", required = true) List<Long> productIds) {
        return memberCouponService.findMemberCouponListForOrder(userId, productIds);
    }

    /**
     * 查询单个优惠劵信息
     *
     * @param userId
     * @param couponId
     * @return MemberCouponVo
     * @Author lcysike
     * @Date 2019/12/19
     */
    @GetMapping("get/coupon")
    @EscapeLogin
    public MemberCouponVo findMemberCouponForOrder(@RequestParam(value = "userId", required = true) String userId, @RequestParam(value = "couponId", required = true) Long couponId) {
        return memberCouponService.findMemberCouponForOrder(userId, couponId);
    }

    /**
     * 查询可使用的满减活动
     *
     * @param productIds
     * @return com.medusa.gruul.common.core.util.Result<java.util.List < com.medusa.gruul.discount.api.model.vo.manager.FullScaleVo>>
     */
    @GetMapping("get/full/scale/single")
    @EscapeLogin
    public List<FullScaleVo> findFullScaleListForOrder(@RequestParam(value = "productIds", required = true) Collection<Long> productIds) {
        return fullScaleService.findFullScaleListForOrder(productIds);
    }

    /**
     * 获取单张满减活动信息
     *
     * @param id
     * @return com.medusa.gruul.discount.api.model.vo.manager.FullScaleVo
     * @author alan
     * @date 2019/12/29 15:44
     */
    @GetMapping("/get/full/scale/{id}")
    @EscapeLogin
    public FullScaleVo findFullScaleForOrder(@ApiParam(value = "满减活动id", required = true) @PathVariable("id") Long id) {
        return fullScaleService.getFullScaleById(id);
    }

    /**
     * 使用优惠劵
     *
     * @param id
     * @return com.medusa.gruul.common.core.util.Result<java.lang.Boolean>
     */
    @PutMapping("use/member/coupon")
    @EscapeLogin
    public Boolean useMemberCouponForOrder(@RequestBody Long id) {
        return memberCouponService.useMemberCouponForOrder(id);
    }

    /**
     * 归还优惠劵
     *
     * @param id
     * @return com.medusa.gruul.common.core.util.Result<java.lang.Boolean>
     */
    @PutMapping("revert/member/coupon")
    @EscapeLogin
    public Boolean revertMemberCouponForOrder(@RequestBody Long id) {
        return memberCouponService.revertMemberCouponForOrder(id);
    }


    /**
     *  领取回收券
     * @param userId  用户id
     * @param couponId 优惠券id
     * @param number 优惠券数量
     * @return
     */
    @GetMapping("grant/discount/coupon")
    @EscapeLogin
    public Result grantDiscountCoupon(@RequestParam(value = "userId", required = true)String  userId,
                                      @RequestParam(value = "couponId", required = true)Long couponId,
                                      @RequestParam(value = "number", required = true)Integer number) {
        return memberCouponService.grantDiscountCoupon(userId,couponId,number);
    }

    //=============================================商品部分===================================================

    /**
     * 查询单个商品可使用优惠劵信息
     *
     * @param userId
     * @return List<ApiCouponVo>
     */
    @GetMapping("get/coupon/product")
    @EscapeLogin
    public List<ApiCouponVo> findMemberCouponListForProduct(@RequestParam(value = "userId", required = true) String userId, @RequestParam(value = "productId", required = true) Long productId) {
        return apiCouponService.findCouponListForProduct(userId, productId);
    }

    /**
     * 查询单个商品可使用满减信息
     *
     * @param productId
     * @return List<FullScaleVo>
     */
    @GetMapping("get/full/scale/product")
    @EscapeLogin
    public List<FullScaleVo> findFullScaleListForProduct(@RequestParam(value = "productId", required = true) Long productId) {
        return fullScaleService.findFullScaleListForProduct(productId);
    }
}
